package org.fei.ClassDiagram.data;

import java.util.List;
import org.fei.ClassDiagram.Editor.Wrappers.ClassDataWrapper;
import org.fei.ClassDiagram.Editor.Wrappers.MethodDataWrapper;
import org.fei.ClassDiagram.Editor.Wrappers.MethodParameterWrapper;

/**
 * Trieda ktora vytvori retazec v spravnom UML formate pre metody tried.
 *
 * @author Tomas
 */
public class MethodData extends Data {

    protected MethodDataWrapper met;
    private boolean isConstructor;
    protected ClassDataWrapper data;
    private String methodPrefix;
    private String methodName;
    private String methodPostfix;

    public MethodData(MethodDataWrapper method, ClassDataWrapper classData) {
        met = method;
        data = classData;
        //Message.showMessage("metoda " + met.getName() + " " + met.getTypeSignature());
        isConstructor = false;

        initMethod();
    }

    @Override
    public String toString() {

        if (met.isConstructor()) {
            isConstructor = true;
            methodName = data.getSimpleName();;
        }

        represent = methodPrefix + methodName + methodPostfix;
        return this.represent;
    }

    public void initMethod() {
        this.methodPrefix = met.accessToString() + " ";

        String metName = met.getName();

        if (met.isConstructor()) {
            metName = data.getSimpleName();
            isConstructor = true;
        }

        this.methodName = metName;

        methodPostfix = "(";

        List<MethodParameterWrapper> params = met.getParameters();

        for (MethodParameterWrapper p : params) {

            //Message.showMessage("parameter: " + p.getName() + " " + p.getDescriptor());
            this.methodPostfix += p.getName() + ": ";
            this.methodPostfix += p.getReturnType();

            if (params.get(params.size() - 1) != p) {
                this.methodPostfix += ", ";
            }
        }

        if (isConstructor) {
            methodPostfix += ")";
        } else if (met.getTypeSignature() == null) {
            methodPostfix += "): " + met.getReturnType();
        } else if (met.getTypeSignature().contains(")T")) {
            String typeSignature = met.getTypeSignature();
            //Message.showMessage(met.getTypeSignature());
            String type = new String();
            boolean start = false;

            for (int i = typeSignature.indexOf(")T"); i < typeSignature.length(); i++) {
                // tu konci
                if (typeSignature.charAt(i) == ';') {
                    start = false;
                    continue;
                }
                // tu sa zapise
                if (start == true) {
                    type += typeSignature.charAt(i);
                }
                // tu zacina
                if (typeSignature.charAt(i) == 'T') {
                    start = true;
                    continue;
                }
            }

            methodPostfix += "): " + type;
        } else {
            methodPostfix += "): " + met.getReturnType();
        }
    }

    public boolean isConstructor() {
        return isConstructor;
    }

    public void setIsConstructor(boolean isConstructor) {
        this.isConstructor = isConstructor;
    }

    public MethodDataWrapper getMethod() {
        return this.met;
    }
}